//
//  AXFTableHeadView.m
//  AXFEightGroup
//
//  Created by hgj on 2016/12/22.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "AXFTableHeadView.h"
#import "JSBadgeView.h"

@interface AXFTableHeadView ()

@property (nonatomic, strong) JSBadgeView *badgeView;

@end

@implementation AXFTableHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self setupButton];
    }
    return self;
}

- (void)setupButton {
    [self addButtonWithTitile:@"我的订单" imageName:@"v2_my_order_icon" tag:MineTopViewButtonTypeOrder];
    [self addButtonWithTitile:@"优惠券" imageName:@"v2_my_coupon_icon" tag:MineTopViewButtonTypeCard];
    [self addButtonWithTitile:@"我的消息" imageName:@"v2_my_message_icon" tag:MineTopViewButtonTypeMessage];
    // 可读性差,可优化
    [self setupLine];
    [self setupLine];
}

- (void)addButtonWithTitile:(NSString *)title imageName:(NSString *)imageName tag:(MineTopViewButtonType)tag {
    
    UIButton *button = [[UIButton alloc] init];
    [button setAttributedTitle:[NSAttributedString mh_imageTextWithImage:[UIImage imageNamed:imageName] imageW:35 imageH:25 title:title fontSize:14 titleColor:[UIColor darkGrayColor] spacing:7] forState:UIControlStateNormal];
    button.tag = tag;
    button.titleLabel.numberOfLines = 0;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button sizeToFit];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    // 假数据
    if ([title  isEqual: @"优惠券"]) {
        self.badgeView = [[JSBadgeView alloc] initWithParentView:button alignment:JSBadgeViewAlignmentTopRight];
        self.badgeView.badgePositionAdjustment = CGPointMake(-55, 10);
        self.badgeView.badgeText = @"1";
        [self.badgeView setNeedsLayout];
    }
}

- (void)setupLine {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor darkGrayColor];
    [self addSubview:view];
}

- (void)layoutSubviews{
    
    //必须要调用super父类的方法
    [super layoutSubviews];
    
    CGFloat width = (SCREEN_WIDTH - 2)/3;
    
    CGFloat height = 60;
    
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull subView, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([subView isKindOfClass:NSClassFromString(@"UIButton")]) {
            
            CGFloat x = idx * width;
            
            subView.frame = CGRectMake(x, 8, width, height);
        }
        
        if ([subView isMemberOfClass:NSClassFromString(@"UIView")]) {
            
            subView.frame = CGRectMake( (idx -2 ) * width, 8, 1, 50);
        }
    }];
    
}

//- (void)layoutSubviews {
//    [super layoutSubviews];
//    CGFloat margin1 = (self.bounds.size.width - self.subviews[0].bounds.size.width * self.subviews.count) / (self.subviews.count + 1);
//    for (int i = 0; i < self.subviews.count - 1; i++) {
//        UIButton *button = self.subviews[i];
//        UIButton *nextButton = self.subviews[i+1];
//        if (i == 0) {
//            [button mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.offset(margin1);
//                make.centerY.equalTo(self);
//            }];
//        }
//        [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(button.mas_right).offset(margin1);
//            make.centerY.equalTo(self);
//        }];
//    }
//}

- (void)clickButton:(UIButton *)button{
    [self.delegate pushToController:button];
}

@end
